<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Common\Constant\Status;
use App\Http\Requests\CouponAddPost;
use App\Libs\Filter\Filter;
use App\Models\Coupon;
use App\Models\CouponGrantList;
use App\Services\ICouponService;
use App\Services\ISystemService;
use Illuminate\Http\Request;

/**
 * 优惠券
 * Class CouponController
 * @package App\Http\Controllers
 */
class CouponController extends Controller
{

    /**
     * 创建优惠券
     * @param Request $request
     */
    public function create(Request $request,ICouponService $couponService)
    {
        if ($request->isMethod('post')){
            $couponService->create($request->input('c_vaildtype'),$request->input('c_name'),$request->input('c_vaildday'),
                $request->input('c_starttime'),$request->input('c_endtime'),$request->input('c_money'),
                $request->input('c_fullmoney'),$request->input('c_count'),$request->input('c_stoptime'));
            return redirect('coupon/all');
        }
        return view('coupon/create',[
            'coupon_vaildtype_days'=>Params::COUPON_VAILDTYPE_DAYS,'coupon_vaildtype_period'=>Params::COUPON_VAILDTYPE_PERIOD
        ]);
    }

    /**
     * 修改优惠券
     * @param Request $request
     */
    public function edit(Coupon $coupon,Request $request,ICouponService $couponService)
    {
        if ($request->isMethod('post')){
            $couponService->edit($coupon->c_id,$request->input('c_vaildtype'),$request->input('c_name'),$request->input('c_vaildday'),
                $request->input('c_starttime'),$request->input('c_endtime'),$request->input('c_money'),
                $request->input('c_fullmoney'),$request->input('c_count'),$request->input('c_stoptime'));
            return redirect('coupon/all');
        }
        return view('coupon/edit',['coupon'=>$coupon,
            'coupon_vaildtype_days'=>Params::COUPON_VAILDTYPE_DAYS,'coupon_vaildtype_period'=>Params::COUPON_VAILDTYPE_PERIOD
        ]);
    }

    /**
     * 所有优惠券列表
     * @param Request $request
     */
    public function all(Request $request,ICouponService $couponService)
    {
        $list = $couponService->all();
        return view('coupon/all',['list'=>$list,]);
    }


    /**
     * 已发放领取列表
     * @param Request $request
     */
    public function receive(Request $request,ICouponService $couponService)
    {
        $filter = Filter::instance()
            ->HTML('用户名：')
            ->input('me_username')
            ->HTML('券号：')
            ->input('cg_number')
            ->HTML('使用的订单号：')
            ->input('cg_user_order_number')
            ->br()->br()
            ->HTML('发放领取时间：')
            ->date('cg_add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $couponService->receiveList($filter->get_where());
        }else{
            $list = $couponService->receiveList();
        }
        return view('coupon/receiveList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 删除领取到的券
     * @param Request $request
     */
    public function delGrant(CouponGrantList $coupon,Request $request,ICouponService $couponService)
    {
        $coupon->delete();
        return back();
    }

    /**
     * 发放优惠券
     * @param Request $request
     */
    public function assign(Request $request,ICouponService $couponService,ISystemService $systemService)
    {
        if ($request->isMethod('post')){
            $ret = $couponService->assign($request->input('merchant_id'),$request->input('coupon_id'),$request->input('count'));
            if ($ret === Status::COUPON_NOT_ENOUGH){
                $systemService->alert(status($ret));
            }else if ($ret){
                $systemService->success('发放成功');
            }
        }
        return back();
    }

}
